
<style lang="less" scoped>
  .page__wrapper {
    width: 100%;
    height: 100%;
  }
</style>

<template>
  <div class="page__wrapper">
    <simple-table :table-column="tableColumn" :table-data="tableData" :table-page-info="tablePageInfo" :table-data-count="tableDataTotalCount" @page-info-change="handlePageInfoChange">
      <template slot="searchSection">
        <simple-form v-model="searchModel" :form-field="searchFormField">
          <template slot="tableOperation">
            <el-button @click="handleDataSearch">{{$t('operation.search')}}</el-button>
            <el-button type="danger" @click="handleLockstatusChange">加解锁</el-button>
            <el-button type="danger" @click="handleImportError">导入异常</el-button>
            <el-button v-if="actionList.indexOf('export') >= 0" type="primary" @click="handleExportTemplate">模版</el-button>
            <el-button v-if="actionList.indexOf('export') >= 0" type="primary" @click="handleExport">导出</el-button>
            <aries-import v-if="actionList.indexOf('import') >= 0" tableName="Biz_Factory_Collective" @on-success="handleImportSuccess"></aries-import>
          </template>
        </simple-form>
      </template>
      <template slot-scope="scope" slot="uploadquarter">
        {{ $getDictLabel({type: '季度', value: scope.row.uploadquarter}) }}
      </template>
      <template slot-scope="scope" slot="companyname">
        {{ scope.row.companyname | companynameFilter(orgList)}}
      </template>
      <template slot-scope="scope" slot="companytype">
        {{ $getDictLabel({type: '公司性质', value: scope.row.companytype}) }}
      </template>
      <template slot-scope="scope" slot="propertytype">
        {{ $getDictLabel({type: '产权性质_厂办', value: scope.row.propertytype}) }}
      </template>
      <template slot-scope="scope" slot="isconfirmation">
        {{ $getDictLabel({type: '是否', value: scope.row.isconfirmation}) }}
      </template>
      <template slot-scope="scope" slot="businessstatus">
        {{ $getDictLabel({type: '经营状态_厂办', value: scope.row.businessstatus}) }}
      </template>
      <template slot-scope="scope" slot="reformmethod">
        {{ $getDictLabel({type: '改革方式_厂办', value: scope.row.reformmethod}) }}
      </template>
      <template slot-scope="scope" slot="isrestructuring">
        {{ $getDictLabel({type: '是否', value: scope.row.isrestructuring}) }}
      </template>
      <template slot-scope="scope" slot="iscompletereform">
        {{ $getDictLabel({type: '是否', value: scope.row.iscompletereform}) }}
      </template>
      <template slot-scope="scope" slot="isinsolvent">
        {{ $getDictLabel({type: '是否', value: scope.row.isinsolvent}) }}
      </template>
      <template slot-scope="scope" slot="profitloss">
        {{ $getDictLabel({type: '盈亏情况', value: scope.row.profitloss}) }}
      </template>
      <template slot="operation" slot-scope="scope">
        <el-button v-if="actionList.indexOf('view') >= 0" circle icon="el-icon-search" :title="$t('operation.view')" @click="handleViewClick(scope.row)"></el-button>
        <el-button v-if="actionList.indexOf('edit') >= 0" :disabled="scope.row.lockstatus === '1'" circle icon="el-icon-edit" :title="$t('operation.edit')" @click="handleEditClick(scope.row)"></el-button>
        <el-button v-if="actionList.indexOf('del') >= 0" :disabled="scope.row.lockstatus === '1'" @click="deleteRecord(scope.row)" circle icon="el-icon-delete" :title="$t('operation.delete')"></el-button>
      </template>
    </simple-table>
    <detail-dialog :show-flag.sync="isShowDetailDialog" :detail-data="rowData" :view-flag="isViewFlag" :ext-data="{sys_mid,orgList}"></detail-dialog>
    <lock-status-dialog :show-flag.sync="isShowLockStatusDialog" :ext-data="{
       sys_mid,
       searchModel: {
         uploadyear: searchModel.uploadyear,
         uploadquarter: searchModel.uploadquarter
       },
       methodName: 'updateWhere',
       objName: 'Biz_Factory_Collective'
      }"></lock-status-dialog>
  </div>
</template>
<script>
import Axios from 'axios'
/** 混入分页信息及数据总数信息 */
import pageMixins from '@/components/base/simple-table/page-mixins'
import detailDialog from './detail'
import lockStatusDialog from './lockStatus'
const BASEURL = {
  list: `/Web/BizController/ajax.html?sys_method=GetListBySql&sys_objName=Biz_Factory_Collective`,
  delete: '/Web/BizController/ajax.html?sys_method=Delete&sys_objName=Biz_Factory_Collective',
  getInitConfig: '/Web/SysAdmin/ajax.html?sys_method=GetInitConfig',
  getCombobox: '/Web/SysAdmin/ajax.html?sys_method=GetCombobox'
}
export default {
  name: 'balance_factory',
  mixins: [pageMixins],
  components: { detailDialog, lockStatusDialog },
  data () {
    return {
      searchModel: {
        uploadyear: this.$moment().format('YYYY'),
        uploadquarter: String(this.$moment().quarter())
      },
      /** 表格数据 */
      tableData: [],
      rowData: {},
      /** 是否显示详情页 dialog */
      isShowDetailDialog: false,
      isShowLockStatusDialog: false,
      /** 是否为查看数据 */
      isViewFlag: false,
      actionList: [],
      sys_mid: '',
      orgList: []
    }
  },
  filters: {
    companynameFilter (value, orgList) {
      let company = orgList.find(item => {
        return item.value === value
      })
      return company ? company.text : ''
    }
  },
  computed: {
    searchFormField () {
      return [
        { prop: 'companyname', label: this.$t('balance_factory.companyname'), type: 'Select', component: { clearable: true, optionList: this.orgList }, col: { span: 5 } },
        { prop: 'uploadyear', label: this.$t('balance_factory.uploadyear'), type: 'Datepicker', component: { clearable: false, type: 'year', valueFormat: 'yyyy' }, col: { span: 5 } },
        { prop: 'uploadquarter', label: this.$t('balance_factory.uploadquarter'), type: 'Select', component: { clearable: false, optionList: this.$getDictList('季度') }, col: { span: 5 } },
        { slotName: 'tableOperation', col: { span: 9, style: { textAlign: 'right' } }, labelWidth: '0' }
      ]
    },
    tableColumn () {
      return [
        { prop: 'companyname', label: this.$t('balance_factory.companyname'), slotName: 'companyname', width: 160, fixed: 'left' },
        { prop: 'uploadyear', label: this.$t('balance_factory.uploadyear'), width: 50, fixed: 'left' },
        { prop: 'uploadquarter', label: this.$t('balance_factory.uploadquarter'), slotName: 'uploadquarter', width: 70, fixed: 'left' },

        // 基本情况
        { label: this.$t('balance_factory.basicSituation'),
          children: [
            { prop: 'hostcompany', label: this.$t('balance_factory.hostcompany'), width: 120 },
            { label: this.$t('balance_factory.domicile'),
              children: [
                { prop: 'province', label: this.$t('balance_factory.province'), width: 100 },
                { prop: 'citycounty', label: this.$t('balance_factory.citycounty'), width: 100 }
              ]
            },
            { prop: 'companytype', label: this.$t('balance_factory.companytype'), slotName: 'companytype', width: 70 },
            { prop: 'propertytype', label: this.$t('balance_factory.propertytype'), slotName: 'propertytype', width: 100 },
            { prop: 'isconfirmation', label: this.$t('balance_factory.isconfirmation'), slotName: 'isconfirmation', width: 70 },
            { prop: 'registeredcapital', label: this.$t('balance_factory.registeredcapital'), width: 100 },
            { prop: 'equitystructure', label: this.$t('balance_factory.equitystructure'), width: 70 },
            { prop: 'managementlevel', label: this.$t('balance_factory.managementlevel'), width: 70 },
            { prop: 'businessstatus', label: this.$t('balance_factory.businessstatus'), slotName: 'businessstatus', width: 70 },
            { prop: 'mainbusiness', label: this.$t('balance_factory.mainbusiness'), width: 70 },
            { prop: 'insurancelocation', label: this.$t('balance_factory.insurancelocation'), width: 270 }
          ]
        },

        // 改革推进情况
        { label: this.$t('balance_factory.progressOfReform'),
          children: [
            { prop: 'reformmethod', label: this.$t('balance_factory.reformmethod'), slotName: 'reformmethod', width: 70 },
            { label: this.$t('balance_factory.theCostOfTheReformsThatHaveOccurred'),
              children: [
                { prop: 'reformcost', label: this.$t('balance_factory.reformcost'), width: 100 },
                { prop: 'assetdebtloss', label: this.$t('balance_factory.assetdebtloss'), width: 120 },
                { prop: 'otherfee', label: this.$t('balance_factory.otherfee'), width: 190 },
                { prop: 'resettlementcost', label: this.$t('balance_factory.resettlementcost'), width: 120 }
              ]
            },
            { prop: 'isrestructuring', label: this.$t('balance_factory.isrestructuring'), slotName: 'isrestructuring', width: 130 },
            { prop: 'iscompletereform', label: this.$t('balance_factory.iscompletereform'), slotName: 'iscompletereform', width: 110 },
            { prop: 'reformmeasuresprogress', label: this.$t('balance_factory.reformmeasuresprogress'), width: 110 }
          ]
        },

        // 资产负债情况
        { label: this.$t('balance_factory.balanceSheet'),
          children: [
            { label: this.$t('balance_factory.theEndOfTheSemester'),
              children: [
                { prop: 'totalassets', label: this.$t('balance_factory.totalassets'), width: 70 },
                { prop: 'netreceivables', label: this.$t('balance_factory.netreceivables'), width: 170 },
                { prop: 'netfixedassets', label: this.$t('balance_factory.netfixedassets'), width: 205 },
                { prop: 'totalliabilities', label: this.$t('balance_factory.totalliabilities'), width: 70 },
                { prop: 'corporateaccountspayable', label: this.$t('balance_factory.corporateaccountspayable'), width: 125 },
                { prop: 'netassets', label: this.$t('balance_factory.netassets'), width: 70 },
                { prop: 'assetsliabilities', label: this.$t('balance_factory.assetsliabilities'), width: 90 },
                { prop: 'isinsolvent', label: this.$t('balance_factory.isinsolvent'), slotName: 'isinsolvent', width: 100 }
              ]
            }

          ]
        },

        // 经营情况
        { label: this.$t('balance_factory.operatingCondition'),
          children: [
            { label: this.$t('balance_factory.thisYearTheCumulative'),
              children: [
                { label: this.$t('balance_factory.operatingIncome'), // 营业收入
                  children: [
                    { prop: 'totalincome', label: this.$t('balance_factory.totalincome'), width: 70 },
                    { prop: 'businessincome', label: this.$t('balance_factory.businessincome'), width: 190 }
                  ]
                },
                { label: this.$t('balance_factory.theCost'), // 成本费用
                  children: [
                    { prop: 'totalcost', label: this.$t('balance_factory.totalcost'), width: 70 },
                    { prop: 'laborcost', label: this.$t('balance_factory.laborcost'), width: 105 },
                    { prop: 'interestexpense', label: this.$t('balance_factory.interestexpense'), width: 70 },
                    { prop: 'depreciationdepletion', label: this.$t('balance_factory.depreciationdepletion'), width: 70 },
                    { prop: 'assetimpairmentloss', label: this.$t('balance_factory.assetimpairmentloss'), width: 95 }
                  ]
                },
                { prop: 'totalprofit', label: this.$t('balance_factory.totalprofit'), width: 70 },
                { prop: 'profitloss', label: this.$t('balance_factory.profitloss'), slotName: 'profitloss', width: 70 }
              ]
            }
          ]
        },

        // 人员情况
        { label: this.$t('balance_factory.theStaff'),
          children: [
            { prop: 'totalemployment', label: this.$t('balance_factory.totalemployment'), width: 100 },
            { label: this.$t('balance_factory.employees'), // 在职员工
              children: [
                { prop: 'totalemployees', label: this.$t('balance_factory.totalemployees'), width: 100 },
                { label: this.$t('balance_factory.sentByTheHostStateOwnedEnterprise'), // 主办国有企业派出
                  children: [
                    { prop: 'sponsorssendsubtotals', label: this.$t('balance_factory.sponsorssendsubtotals'), width: 120 },
                    { prop: 'contractedemployee', label: this.$t('balance_factory.contractedemployee'), width: 85 },
                    { prop: 'marketemployee', label: this.$t('balance_factory.marketemployee'), width: 85 }
                  ]
                },
                { label: this.$t('balance_factory.collectiveEnterpriseEmployeeTitle'), // 集体企业员工
                  children: [
                    { prop: 'collectiveenterpriseemployee', label: this.$t('balance_factory.collectiveenterpriseemployee'), width: 120 },
                    { prop: 'scope1', label: this.$t('balance_factory.scope1'), width: 195 },
                    { prop: 'scope2', label: this.$t('balance_factory.scope2'), width: 200 },
                    { prop: 'scope3', label: this.$t('balance_factory.scope3'), width: 200 },
                    { prop: 'scope4', label: this.$t('balance_factory.scope4'), width: 140 }
                  ]
                },
                { prop: 'laboremployeecurrent', label: this.$t('balance_factory.laboremployeecurrent'), width: 70 }
              ]
            },
            { label: this.$t('balance_factory.retirees'), // 离退休人员
              children: [
                { prop: 'totalretirees', label: this.$t('balance_factory.totalretirees'), width: 110 },
                { prop: 'yeartotalretirees', label: this.$t('balance_factory.yeartotalretirees'), width: 160 }
              ]
            }
          ]
        },

        // 本年累计在职人员变动情况
        { label: this.$t('balance_factory.theChangeOfTheInServicePersonnelAccumulativelyThisYear'),
          children: [
            { label: this.$t('balance_factory.attrition'), // 减员
              children: [
                { prop: 'subtotalreduction', label: this.$t('balance_factory.subtotalreduction'), width: 70 },
                { prop: 'naturalreduction', label: this.$t('balance_factory.naturalreduction'), width: 70 },
                { prop: 'agreementcancellation', label: this.$t('balance_factory.agreementcancellation'), width: 120 },
                { prop: 'earlyretirement', label: this.$t('balance_factory.earlyretirement'), width: 70 },
                { prop: 'otherreduction', label: this.$t('balance_factory.otherreduction'), width: 70 }
              ]
            },
            { label: this.$t('balance_factory.increase'), // 增员
              children: [
                { prop: 'subtotalincrease', label: this.$t('balance_factory.subtotalincrease'), width: 70 },
                { prop: 'mainbusinessemployee', label: this.$t('balance_factory.mainbusinessemployee'), width: 95 },
                { prop: 'recruitmentmarketemployee', label: this.$t('balance_factory.recruitmentmarketemployee'), width: 120 },
                { prop: 'laboremployeeincrease', label: this.$t('balance_factory.laboremployeeincrease'), width: 70 },
                { prop: 'otherincease', label: this.$t('balance_factory.otherincease'), width: 70 }
              ]
            }
          ]
        },

        // 人员分流安置情况
        { label: this.$t('balance_factory.personnelDiversionAndResettlement'),
          children: [
            { label: this.$t('balance_factory.resettlementNumber'), // 分流安置人数（包括协议解除劳动合同、提前退休及以下形式）
              children: [
                { prop: 'totalstaffing', label: this.$t('balance_factory.totalstaffing'), width: 290 },
                { prop: 'internalretirement', label: this.$t('balance_factory.internalretirement'), width: 70 },
                { prop: 'leavejob', label: this.$t('balance_factory.leavejob'), width: 70 },
                { prop: 'agreementreservation', label: this.$t('balance_factory.agreementreservation'), width: 120 },
                { prop: 'othertype', label: this.$t('balance_factory.othertype'), width: 70 },
                { prop: 'othertypedesc', label: this.$t('balance_factory.othertypedesc'), width: 95 }
              ]
            },
            { prop: 'cumulativedefault', label: this.$t('balance_factory.cumulativedefault'), width: 210 }
          ]
        },

        { label: this.$t('field.operation'), slotName: 'operation', width: 100, fixed: 'right', align: 'center' }
      ]
    }
  },
  /**
   * 注入到子组件
   */
  provide () {
    return { handleDataSearch: this.handleDataSearch }
  },
  methods: {
    handleDataSearch () {
      let params = Object.assign({}, this.tablePageInfo, { sys_mid: this.sys_mid, UserID: this.$store.state.app.userInfo.userid }, { sys_search: this.searchModel })

      let loadingFlag = this.$loading({ fullscreen: false, target: this.$el, text: this.$t('tip.systemLoading') })
      this.$axios
        .post(BASEURL.list, params)
        .then(resp => {
          this.tableData = resp.rows
          this.tableDataTotalCount = resp.total
        })
        .finally(() => {
          loadingFlag.close()
        })
    },
    handleInitData () {
      let configLoadingFlag = this.$loading({ fullscreen: false, target: this.$el, text: this.$t('tip.systemLoading') })
      this.$axios.post(BASEURL.getInitConfig)
        .then(resp => {
          this.actionList = resp.actionKeys.split(',')
          this.sys_mid = resp.mid

          /** 设置列表及涉及主数据请求参数 */
          let params = Object.assign({}, this.tablePageInfo, { sys_mid: resp.mid, UserID: this.$store.state.app.userInfo.userid })
          let comboParams = { sys_json: JSON.stringify([{ ObjName: 'C_SYS_Role' }, { ObjName: 'C_SYS_Org' }]) }
          let loadingFlag = this.$loading({ fullscreen: false, target: this.$el, text: this.$t('tip.systemLoading') })

          Axios.all([
            this.$axios.post(BASEURL.getCombobox, comboParams),
            this.$axios.post(BASEURL.list, params)
          ])
            .then(Axios.spread((comboData, pageData) => {
              this.orgList = comboData.C_SYS_Org

              this.tableData = pageData.rows
              this.tableDataTotalCount = pageData.total
            }))
            .finally(() => {
              loadingFlag.close()
            })
        })
        .finally(() => {
          configLoadingFlag.close()
        })
    },
    handleAddClick () {
      this.rowData = {}
      this.isViewFlag = false
      this.isShowDetailDialog = true
    },
    handleViewClick (row) {
      this.rowData = this.$_.cloneDeep(row)
      this.isViewFlag = true
      this.isShowDetailDialog = true
    },
    handleEditClick (row) {
      this.rowData = this.$_.cloneDeep(row)
      this.isViewFlag = false
      this.isShowDetailDialog = true
    },
    deleteRecord (row) {
      this.$msgbox({
        title: this.$t('tip.warning'),
        message: this.$t('tip.confirmDelete'),
        type: 'warning',
        showCancelButton: true,
        closeOnClickModal: false,
        closeOnPressEscape: false,
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            this.$axios
              .post(BASEURL.delete, { id: row.id, idField: 'id', sys_mid: this.sys_mid })
              .then(resp => {
                this.$message({ type: 'success', message: this.$t('tip.deleteSuccess') })
                done()
              })
              .finally(() => {
                instance.confirmButtonLoading = false
              })
          } else done()
        }
      })
        .then(() => {
          /** 如果本页面中只有一条数据 */
          /** 手动触发 simple-table 组件中的 watch，从而调用 mixins 中的请求数据方法 */
          if (this.tableData.length === 1) {
            this.tablePageInfo.page--
          } else {
            this.handleDataSearch()
          }
        })
    },
    handleLockstatusChange () {
      this.isShowLockStatusDialog = true
    },
    handleImportError () {
      this.$router.push({
        name: 'log_import_factory',
        query: {
          objName: 'Biz_Factory_Collective'
        }
      })
    },
    handleImportSuccess (resp) {
      this.handleDataSearch()
    },
    handleExportTemplate () {
      this.$utils.fileExportAs('/Web/SysAdmin/ajax.html', {
        sys_objName: 'Biz_Factory_Collective',
        sys_tableName: 'Biz_Factory_Collective',
        sys_method: 'ExcelTemplate',
        sys_mid: this.sys_mid
      }, '厂办大集体入系统模版.xls')
    },
    handleExport () {
      this.$utils.fileExportAs('/Web/BizController/ajax.html', {
        sys_objName: 'Export_Factory_Collective',
        sys_tableName: 'Export_Factory_Collective',
        sys_method: 'QueryExport',
        sys_mid: this.sys_mid,
        sys_search: this.searchModel,
        // 业务部分······
        dataStartRowIndex: 6, // 导出的excel对应的数据起始行
        dataStartCollumIndex: 0, // 导出的excel对应的数据起始列
        specialInfo: `${this.searchModel.uploadyear}年${this.$getDictLabel({ type: '季度', value: this.searchModel.uploadquarter })}        单位：万元，人`,
        specialStartRowIndex: 1, // 定位表头坐标
        specialStartCollumIndex: 0 // 定位表头坐标
      }, '厂办大集体入系统.xls')
    }
  },
  mounted () {
    this.handleInitData()
  }
}
</script>
